package com.learn.project.plan.bean;

import com.uptool.core.util.ArrayUtil;
import com.uptool.io.util.FileUtil;



import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

/**
 * @Description: 工厂
 * @Author: chenfuyuan
 * @Date: 2021/4/27 21:12
 */
public class LearnPlanFactory implements PlanFactory{


    @Override
    public Plan getPlan() throws IOException, ClassNotFoundException {
        File objectFile = LearnTask.FILE;
        if (!objectFile.exists()){
            return initLearnPlan();
        }
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(objectFile));
        return (Plan) objectInputStream.readObject();
    }

    private Plan initLearnPlan() throws IOException {
        File file = FileUtil.getFile("E:/exe/learn-plan/learnPlan.txt");

        String[] stringByFile = FileUtil.getStringByFile(file);
        Object[][] taskListStr = ArrayUtil.quallyDivide(FileUtil.getStringByFile(file),LearnTask.FIELD_SIZE);

        Plan result = new CyclePlan();

        Task[] taskList = new Task[taskListStr.length];
        int i = 0;
        for (Object[] taskStr : taskListStr) {
            Task task = new LearnTask();
            task.setDetail((String) taskStr[0]);
            task.setTime((String) taskStr[1]);
            taskList[i++] = task;
        }

        result.setTasks(taskList);


        return result;
    }
}
